package com.keyancompany.zhongbaopingtai.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.BalanceHistoryAdapter;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BalanceHistoryModel;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.FindModel;
import com.keyancompany.zhongbaopingtai.weight.IListView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 资金流水
 */
public class BalanceHistoryActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    String title = "资金流水";
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.list_view)
    IListView listView;

    @Bind(R.id.find_swipe_layout)
    SwipeRefreshLayout swipeLayout;

    BalanceHistoryAdapter mAdapter;
    private int currentPage;
    private int total;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_banlance_history;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        initSwipeLayout(swipeLayout);
        swipeLayout.setOnRefreshListener(this);


        mAdapter = new BalanceHistoryAdapter(this);
        listView.setAdapter(mAdapter);
        listView.setStateListener(new IListView.SwipeLayoutState() {
            @Override
            public boolean isRefresh() {
                return swipeLayout.isRefreshing();
            }

            @Override
            public boolean isLastPage() {
                return total == currentPage;
            }

            @Override
            public void requestNextPage() {
                requestTask(++currentPage);
            }
        });
        swipeLayout.post(new Thread(new Runnable() {

            @Override
            public void run() {
                swipeLayout.setRefreshing(true);
                onRefresh();
            }
        }));
    }

    private void requestTask(int i) {
        UserAPI.paylogs(currentPage + "", this, null);
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        requestTask(currentPage);
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        swipeLayout.setRefreshing(false);
        if (currentPage == 1) {
            mAdapter.clear();
        } else {

        }

        if (type == UserAPI.PAY_LOGS) {
            //{"result":"success","data":{"total":1,"current":1,"list":
            // [{"summary":1,"phone":"18600624024","setlevel":"0","invoice":"","tel":"",
            // "endTime":"2016-10-31","milestone_fill":0,"startTime":"2016-09-30 08:53:53",
            BaseModel base = JSONObject.parseObject(json, BaseModel.class);
            Log.e("FindFragment", " ---- " + base.toString());
            if (base.isSuccess()) {
                JSONObject object = JSONObject.parseObject(base.data);
                currentPage = object.getInteger(Constants.CURRENT);
                total = object.getInteger(Constants.TOTAL);
                List<BalanceHistoryModel> list = JSONObject.parseArray(object.getString("list"), BalanceHistoryModel.class);
                if (mAdapter == null) return;
                listView.loadMoreFinish();
                if (total == currentPage) {
                    listView.removeFooterView();
                }
                mAdapter.addAllAfter(list);
            }
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        swipeLayout.setRefreshing(false);
    }
}
